package com.servlet;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mode.Glasses;
import com.mode.Shoes;
import com.mode.YiFu;

/**
 * Servlet implementation class shoppingServlet1
 */
@WebServlet("/shoesServlet")
public class shoesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public shoesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession(true);
		String name=request.getParameter("name");
		String price=request.getParameter("price");
		String num=request.getParameter("num");
//		System.out.println(name+price+num);
		
		if(session.getAttribute("list")==null){
			List<Object> list=new ArrayList<>();
			Shoes shoes=new Shoes();
			shoes.setName("name");
			shoes.setPrice(Integer.valueOf(price));
			shoes.setNum(Integer.valueOf(num));
			list.add(shoes);
			session.setAttribute("list", list);
		}else{
			List<Object> list=(List<Object>) session.getAttribute("list");
			Shoes shoes=new Shoes();
			shoes.setName("name");
			shoes.setPrice(Integer.valueOf(price));
			shoes.setNum(Integer.valueOf(num));
			list.add(shoes);
			session.setAttribute("list", list);
		}
		
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out=response.getWriter();
		
		List<Object> list=(List<Object>) session.getAttribute("list");
		for (Object obj : list) {
			String className=obj.getClass().getSimpleName();
			if(className.equals("YiFu")){
				YiFu yiFu=(YiFu) obj;
				out.write(yiFu.getName()+"\t"+yiFu.getPrice()+"\t"+yiFu.getNum());
			}
			if(className.equals("Glasses")){
				 Glasses glasses=(Glasses) obj;
				 out.write(glasses.getName()+"\t"+glasses.getPrice()+"\t"+glasses.getNum());
			}
			if(className.equals("Shoes")){
				Shoes shoes=(Shoes) obj;
				out.write(shoes.getName() + "\t" + shoes.getPrice() + "\t" + shoes.getNum());
			}
			   
		}
		request.getRequestDispatcher("tiaoZhuan.jsp").forward(request, response);
//		response.sendRedirect("shoes.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
